package io.github.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.github.dao.SysLogDao;
import io.github.entity.SysLogEntity;
import io.github.service.SysLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Max
 * @Date: 2019/10/8 15:29
 */

@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogDao, SysLogEntity> implements SysLogService {

    @Resource
    private SysLogDao dao;

    @Override
    public void save(SysLogEntity entity) {
        baseMapper.insert(entity);
    }

    @Override
    public Page<SysLogEntity> query(Integer offset,Integer limit,String startTime, String endTime, Integer action,String userName) {
        Wrapper<SysLogEntity> wrapper = new EntityWrapper<>();

        if(action !=null){
            wrapper.eq("action",action);
        }

        if(StringUtils.isNoneBlank(startTime,endTime)){
            wrapper.between("create_time",startTime,endTime);
        }

        if(StringUtils.isNotBlank(userName)){
            wrapper.like("user_name",userName);
        }

        wrapper.orderBy("id",false);

        Page<SysLogEntity> page = new Page<>(offset,limit);
        return this.selectPage(page,wrapper);
    }
}
